package com.beg.cant.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.List;

@Data
@Schema(description = "分页响应结果")
public class PageResponse<T> {
    @Schema(description = "当前页码")
    private Integer pageNum;

    @Schema(description = "每页数量")
    private Integer pageSize;

    @Schema(description = "总记录数")
    private Long total;

    @Schema(description = "总页数")
    private Integer pages;

    @Schema(description = "数据列表")
    private List<T> list;

    public static <T> PageResponse<T> of(List<T> list, long total, PageRequest pageRequest) {
        PageResponse<T> response = new PageResponse<>();
        response.setList(list);
        response.setTotal(total);
        response.setPageNum(pageRequest.getPageNum());
        response.setPageSize(pageRequest.getPageSize());
        response.setPages((int) ((total + pageRequest.getPageSize() - 1) / pageRequest.getPageSize()));
        return response;
    }
}
